﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Sripirom.ATS.Core.Logging;

namespace Sripirom.ATS.Core.Validator
{
    /// <summary>
    /// Vlaidates objects that implement <see cref="IValidatatableObject"/>
    /// </summary>
    public class ValidatableObjectValidator : IObjectValidator
    {
        /// <summary>
        /// Validates the object.
        /// </summary>
        /// <param name="value">The object to validate.</param>
        /// <returns>
        ///     A collection of <see cref="ValidateResult"/> containing information
        ///     about validation errors
        /// </returns>
        public IEnumerable<ValidationResult> Validate(object value)
        {
            LoggerFactory.CreateLog().Debug("I:Validate");

            var validateableInput = value as IValidatableObject;

            if (validateableInput != null)
            {
                var context = new ValidationContext(value, null, null);

                foreach (var result in validateableInput.Validate(context))
                    yield return result;
            }

            LoggerFactory.CreateLog().Debug("O:Validate");
        }
    }
}